using System;
using System.Threading;
using SLS.ExClassLib.Contract;
using SLS.ExClassLib.Event;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.Message;
namespace LanSunStudio.Service.WorkFlowService
{ 
    partial class WorkFlowCenterService 
    {

        private void ReplybyCallBack(MessageEx msg)
        {
            try
            {
                /*
                string AgentPath = m_CenterUICallBackPath;
                AgentCallback = (IWorkFlowCenterCallBack)m_AgentcallbackList[AgentPath];
                */
                ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state)
                {
                    AgentCallback.NotifyWFClient(msg);
                }), null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
       public void ReceiveMessage(MessageEx msg)
        {
            try
            {
                Guid WFUID = (Guid)msg.Tag;
                string JobType = msg.JobType;
                string JobID = msg.JobID;
                string JobContent = msg.JobContent;
                RegisterMessage(msg);
                switch (JobID)
                {
                    case "OnTrackingCommand":
                        /*
                        //trackingCommand = (TrackingCommand)msg.WFTrace;                        
                        sqlConnection.Open();
                        SqlCommand sqlCommand = sqlConnection.CreateCommand();
                        foreach (SqlParameter parameter in trackingCommand.Parameters)
                        {
                            sqlCommand.Parameters.Add(parameter);
                        } 
                        sqlCommand.Transaction = sqlConnection.BeginTransaction();
                        sqlCommand.CommandText = trackingCommand.Procedure;
                        sqlCommand.CommandType = CommandType.StoredProcedure;
                        sqlCommand.ExecuteNonQuery();
                        sqlCommand.Transaction.Commit();
                        sqlConnection.Close();
                        */
                        break;
                    case "OnStartWorkFlow":
                        WFUID = StartWFInstance(msg);
                        break;
                    case "OnStartChildWorkFlow":
                        break;
                    case "OnEndWithReturn": 
                        break;
                    default:
                        RaiseEvent(msg, (Guid)msg.Tag);
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void SendMessage(MessageEx msg)
        {
            try
            {
                string JobType = msg.JobType;
                if (JobType == "WF2UI")
                {
                    SendUIMessage(msg);
                }
                if (JobType == "WF2WF")
                {
                    SendWFMessage(msg);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void SendUIMessage(MessageEx msg)
        {
            try
            {
                // Subscribe the guest to the beer inventory
                MessageEx msg_in = msg;
                Guid WFUID =(Guid) msg_in.Tag;
                string JobType = msg_in.JobType;
                string JobID = msg_in.JobID;
                string JobContent = msg_in.JobContent;
                RegisterMessage(msg_in);
                if (WorkFlowList.ContainsKey(WFUID))
                {
                    m_StateMachineWFInstance = WorkFlowList[WFUID];
                    string WFType = m_StateMachineWFInstance.StateMachineWorkflow.GetType().ToString();
                    //msg_in.WFType = WFType;
                }

                switch (JobContent)
                {

                    case "ReturnResult":
                        /*
                        MessageEx Message_Return = MessagePool.MessageStock[msg_in.MessageID_Return].msg;
                        msg_in.JobID = Message_Return.JobID;
                        msg_in.JobContent = Message_Return.JobContent;
                        msg_in.JobType = "WF4UI";
                        msg_in.Address = Message_Return.Address;
                        ReplybyCallBack(msg_in);
                        */
                        break;
                    default:
                        ReplybyCallBack(msg_in);
                        break;
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void SendWFMessage(MessageEx msg)
        {
            try
            {
                MessageEx msg_in = msg;
                Guid WFUID = Guid.NewGuid();// //msg_in.WFInstance_ID;
                string JobType = msg_in.JobType;
                string JobID = msg_in.JobID;
                string JobContent = msg_in.JobContent;

                RegisterMessage(msg_in);
                if (WorkFlowList.ContainsKey(WFUID))
                {
                    m_StateMachineWFInstance = WorkFlowList[WFUID];
                    string WFType = m_StateMachineWFInstance.StateMachineWorkflow.GetType().ToString();
                    //msg_in.WFType = WFType;
                }

                switch (JobID)
                {
                    case "OnStartChildWorkFlow":
                        WFUID = StartWFInstance(msg_in);
                        break;
                    case "OnEndWithReturn":
                        /*{
                            if (MessagePool.MessageStock.ContainsKey(msg_in.WFMessageID_Req))
                            {
                                MessageEx WFMsgReurn = MessagePool.MessageStock[msg_in.WFMessageID_Req].msg;

                                Guid WFInstance_ID_Req = WFMsgReurn.WFInstance_ID;
                                msg_in.JobID = WFMsgReurn.JobID;
                                msg_in.JobContent = WFMsgReurn.JobContent;
                                RaiseEvent(msg_in, WFInstance_ID_Req);
                            }
                        }*/
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void RegisterCallBackPath(IWorkFlowCenterCallBack Address, MessageEx msgRequest, bool CenterUI)
        {
            string m_AgentPath = msgRequest.Address;
            try
            {
                if (m_AgentPath == null)
                {
                    return;
                }
                if (!m_AgentcallbackList.ContainsKey(m_AgentPath))
                {
                    m_AgentcallbackList.Add(m_AgentPath, Address);
                }
                if (CenterUI)
                {
                    m_CenterUICallBackPath = m_AgentPath;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        public void RegisterMessage(MessageEx msg)
        {
            /*
            if (msg.MessageID_Return != "" || msg.WFMessageID_Req != "")
            {
                if (!MessagePool.MessageStock.ContainsKey(msg.MessageID))
                {
                    MessagePool.Add(msg);
                }
            }
            */
        }
        private Guid StartWFInstance(MessageEx msg)
        { 
            try
            {
                
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return wfinstance.InstanceId;
        }
        public void CallUI(ExMessageExEventArgs e)
        {
            try
            {
                MessageEx msg = e.MsgExRequest;
                EventDispatcher.Instance.Publish(msg, null, EventType.WF2UI);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void CallWF(ExMessageExEventArgs e)
        {
            try
            {
                MessageEx msg = e.MsgExRequest;
                EventDispatcher.Instance.Publish(msg, null, EventType.WF2WF);  
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
    } 
}
